/**
 * There are <a href="https://github.com/thinkgem/jeesite">JeeSite</a> code generation
 */
package com.chlitina.store.modules.expert.service;

import java.util.Date;
import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.chlitina.store.common.persistence.Page;
import com.chlitina.store.common.service.BaseService;
import com.chlitina.store.common.utils.DateUtils;
import com.chlitina.store.modules.expert.dao.StoreCustomerNotifyDao;
import com.chlitina.store.modules.expert.entity.StoreCustomerNotify;
import com.chlitina.store.modules.sms.utils.SmsUtils;

/**
 * 客户产品护理提醒Service
 * 
 * @author liujiang
 * @version 2014-02-19
 */
@Component
@Transactional(readOnly = true)
public class StoreCustomerNotifyService extends BaseService {

	private static Logger logger = LoggerFactory
			.getLogger(StoreCustomerNotifyService.class);

	@Autowired
	private StoreCustomerNotifyDao storeCustomerNotifyDao;

	public StoreCustomerNotify get(Long id) {
		return storeCustomerNotifyDao.findOne(id);
	}

	public Page<StoreCustomerNotify> find(Page<StoreCustomerNotify> page,
			StoreCustomerNotify storeCustomerNotify) {
		DetachedCriteria dc = storeCustomerNotifyDao.createDetachedCriteria();
		dc.addOrder(Order.desc("id"));
		return storeCustomerNotifyDao.find(page, dc);
	}

	@Transactional(readOnly = false)
	public List<StoreCustomerNotify> findNotSendBySolutionProduct(Long storeId,
			Date startDate, Date endDate, long customerID, long solutionId,
			long productID) {
		DetachedCriteria dc = storeCustomerNotifyDao.createDetachedCriteria();
		dc.createAlias("smsSendQueue", "sdq");
		dc.createAlias("customerSolution", "cs");
		try {
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		dc.add(Restrictions.eq("store.id", storeId));
		dc.add(Restrictions.between("notifyDate", startDate, endDate));
		dc.add(Restrictions.eq("customer.id", customerID));
		dc.add(Restrictions.eq("cs.id", solutionId));
		dc.add(Restrictions.eq("storeProduct.id", productID));
		dc.add(Restrictions.eq("sdq.status", -1));
		return storeCustomerNotifyDao.find(dc);
	}

	@Transactional(readOnly = false)
	public List<StoreCustomerNotify> findByParameter(Long storeId,
			String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getEndOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return storeCustomerNotifyDao.findByParameter(storeId, startDate,
				endDate);
	}

	@Transactional(readOnly = false)
	public Long getRealCount(Long storeId, String start, String end) {
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getBeginOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		return storeCustomerNotifyDao.countByParameter(SmsUtils.default_length,
				storeId, startDate, endDate);
	}

	@Transactional(readOnly = false)
	public Long getSendCount(Long storeId, String start, String end) {
		DetachedCriteria dc = storeCustomerNotifyDao.createDetachedCriteria();
		Date startDate = null;
		Date endDate = null;
		try {
			startDate = DateUtils.parseDate(start);
			startDate = DateUtils.getBeginOfDay(startDate);
			endDate = DateUtils.parseDate(end);
			endDate = DateUtils.getBeginOfDay(endDate);
		} catch (Exception e) {
			logger.debug("日期转换异常");
		}
		dc.add(Restrictions.eq("store.id", storeId));
		dc.add(Restrictions.between("notifyDate", startDate, endDate));
		return storeCustomerNotifyDao.count(dc);
	}

	@Transactional(readOnly = false)
	public void save(StoreCustomerNotify storeCustomerNotify) {
		storeCustomerNotifyDao.save(storeCustomerNotify);
	}

	@Transactional(readOnly = false)
	public void delete(Long id) {
		storeCustomerNotifyDao.delete(id);
	}

}
